/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import entity.Publisher;
import iface.IManagerDB;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import manager.DisabledPublisherManager;
import manager.PublisherManager;

/**
 *
 * @author User
 */
public class PublisherDB implements IManagerDB<PublisherManager, DisabledPublisherManager> {

    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;

    public PublisherDB(Connection conn) {
        this.conn = conn;
    }

    @Override
    public void loadData(PublisherManager t, boolean value) {
        try {
            cstmt = conn.prepareCall("{call sp_GetPublisher}");
            cstmt.setBoolean("Status", value);
            rs = cstmt.executeQuery();
            while (rs.next()) {
                int publisherID = rs.getInt("PublisherID");
                String publisherName = rs.getString("PublisherName");
                boolean status = rs.getBoolean("Status");

                Publisher publisher = new Publisher(publisherID, publisherName, status);
                t.add(publisher);
            }
        } catch (SQLException ex) {
            Logger.getLogger(PublisherDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void insert(Object object) {
        Publisher publisher = (Publisher) object;
        try {
            cstmt = conn.prepareCall("{call sp_InsertOrder(?,?,?)}");
            cstmt.setInt(1, publisher.getPublisherID());
            cstmt.setString(2, publisher.getPublisherName());
            cstmt.setBoolean(3, publisher.isStatus());


            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(PublisherDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update(Object object) {
        Publisher publisher = (Publisher) object;
        try {
            cstmt = conn.prepareCall("{call sp_UpdateOrder(?,?,?)}");
            cstmt.setInt(1, publisher.getPublisherID());
            cstmt.setString(2, publisher.getPublisherName());
            cstmt.setBoolean(3, publisher.isStatus());


            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(PublisherDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void delete(Object object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
